﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Scan.Controls;
using Scan.PDAService;

namespace Scan
{
    public partial class VouchStockInPut : BaseView
    {
       // Company gVendorEntity = null;
        Warehouse gWarehouse = null;
        Person gPerson = null;
        Inventory gInventory = null;

        public VouchStockInPut()
        {
            InitializeComponent();
            this.Title = "采购入库单";
            this.AcceptScan = true;
        }

        public override void OnLoad()
        {
            txtVendor.Focus();
        }

        // 档案选择窗口返回调用此过程，返回数据。
        public override void ReturnProc(Boolean Successed, ReferType ReferValue, object ParamObject)
        {
            switch (ReferValue)
            {
            //    case ReferType.Company:     // 供应商参照返回
            //        {
            //            gVendorEntity = (Company)ParamObject;
            //            if (gVendorEntity != null)
            //                txtVendor.Text = gVendorEntity.CComName;
            //        }
            //        break;
                case ReferType.Warehouse:  // 仓库参照返回
                    {
                        gWarehouse = (Warehouse)ParamObject;
                        if (gWarehouse != null)
                            txtWarehouse.Text = gWarehouse.CWhName;
                    }
                    break;
                case ReferType.Person:     // 人员参照返回
                    {
                        gPerson = (Person)ParamObject;
                        if (gPerson != null)
                            txtPerson.Text = gPerson.CPersonName;
                    }
                    break;
                case ReferType.Inventory:  // 商品参照返回
                    {
                        gInventory = (Inventory)ParamObject;
                        if (gInventory != null)
                            DataBind(gInventory);
                    }
                    break;
            }
        }

        // 返回
        public override void Escape(object sender, EventArgs e)
        {
            this.ParentForm.LoadView(this.ResultView.GetType(), null, true, false);
        }

        // 取消，S1键
        public override void S1(object sender, EventArgs e)
        {
            if (tabVouch.SelectedIndex == 0)
            {
                this.ParentForm.LoadView(typeof(MainMenuView), this, true, false);
            }
        }

        // 保存，S2键
        public override void S2(object sender, EventArgs e)
        {
            if (tabVouch.SelectedIndex == 0)
            {
                this.ParentForm.LoadView(typeof(MainMenuView), this, true, false);
            }
            else
            {
                btnScanOK.PerformClick();
            }
        }

        // 切换至单据信息
        public override void F3(object sender, EventArgs e)
        {
            tabVouch.SelectedIndex = 0;
        }

        // 切换至扫描商品
        public override void F6(object sender, EventArgs e)
        {
            tabVouch.SelectedIndex = 1;
            txtBarCode.Focus();
        }

        /// <summary>
        /// 获取扫描的条码
        /// </summary>
        /// <param name="scanStr"></param>
        public override void OnGetBarcode(string scanStr)
        {
            //txtBarCode.Text = scanStr;

            Inventory[] Inventorys = null;
            Inventorys = ArchivesInventorys.GetInventorys(false, scanStr, "", "");
            if (Inventorys != null && Inventorys.Length > 0)
            {
                // 判断是否有条码重复的商品
                if (Inventorys.Length == 1)
                {
                    gInventory = Inventorys[0];
                    DataBind(gInventory);
                }
                else //如有条码重复的商品，弹出窗口供选择
                {
                    this.ParentForm.LoadView(typeof(ReferInventoryView), this, true, false);
                    ReferInventoryView InventoryView = (ReferInventoryView)this.ParentForm.FindViewEx(typeof(ReferInventoryView));
                    InventoryView.Inventorys = Inventorys;
                    InventoryView.DataBind(Inventorys);
                }
            }
            else
            {
                MessageBox.Show("没有获取到该条码的商品！", this.Title);
            }
        }

        // 绑定表格数据
        public void DataBind(Inventory Inventory)
        {
            ListViewItem lvItem = null;
            if (Inventory != null)
            {
                // 判断上次扫描是否有保存
                //if (txtBarCode.Text != string.Empty)
                //{
                //    AddInventoryList();
                //}
                //txtBarCode.Text = Inventory.BarCode;
                //txtInvName.Text = Inventory.InvName;
                //txtInvStd.Text = Inventory.InvStd;
                //txtInvType.Text = Inventory.InvType;
                //txtAssitUnitRate.Text = Inventory.AssitUnitRate.ToString();
                //txtQuantity.Text = "1";
                //txtAssitQuantity.Text = "0";
                //cbbUnitName.Items.Clear();
                //cbbUnitName.Items.Add(Inventory.UnitName);
                //cbbUnitName.Items.Add(Inventory.AssitUnitName);
                //if (Inventory.DefautIN)
                //    cbbUnitName.SelectedIndex = 1;
                //else
                //    cbbUnitName.SelectedIndex = 0;


            }
            else
            {
                MessageBox.Show("商品数据为空！", this.Title);
            }
        }

        // 供应商参照 F2
        private void txtVendor_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 113)
            {
                btnFindVendor.PerformClick();
            }
        }

        private void btnFindVendor_Click(object sender, EventArgs e)
        {
            try
            {
                ReferCompanyView CompanyView = (ReferCompanyView)this.ParentForm.FindViewEx(typeof(ReferCompanyView));
                CompanyView.Title = "供应商选择";
                this.ParentForm.LoadView(typeof(ReferCompanyView), this, true, false);
                CompanyView.Find("", "");
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取供应商档案出错：" + ex.Message, "出错提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            }
        }

        // 仓库档案参照 F2
        private void txtWarehouse_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 113)
            {
                btnFindWarehouse.PerformClick();
            }
        }

        private void btnFindWarehouse_Click(object sender, EventArgs e)
        {
            try
            {
                this.ParentForm.LoadView(typeof(ReferWarehouseView), this, true, false);
                ReferWarehouseView WarehouseView = (ReferWarehouseView)this.ParentForm.FindViewEx(typeof(ReferWarehouseView));
                WarehouseView.Find("", txtWarehouse.Text.Trim());
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取仓库档案出错：" + ex.Message, "出错提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            }
        }

        // 人员档案参照 F2
        private void txtPerson_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 113)
            {
                btnFindPerson.PerformClick();
            }
        }

        private void btnFindPerson_Click(object sender, EventArgs e)
        {
            try
            {
                this.ParentForm.LoadView(typeof(ReferPersonView), this, true, false);
                ReferPersonView PersonView = (ReferPersonView)this.ParentForm.FindViewEx(typeof(ReferPersonView));
                PersonView.Find("", txtPerson.Text.Trim());
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取人员档案出错：" + ex.Message, "出错提示");
            }
        }

        // 商品参照选择 F2
        private void txtBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 113)
            {
                btnInventory.PerformClick();
            }
        }

        private void btnInventory_Click(object sender, EventArgs e)
        {
            try
            {
                this.ParentForm.ShowProgress("获取商品档案", false);
                this.ParentForm.LoadView(typeof(ReferInventoryView), this, true, false);
                ReferInventoryView InventoryView = (ReferInventoryView)this.ParentForm.FindViewEx(typeof(ReferInventoryView));
                InventoryView.Find(txtBarCode.Text.Trim(), "", "");
                this.ParentForm.HideProgress();
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取商品档案出错：" + ex.Message, "出错提示");
            }
        }

        // 只能录入数字
        private void txtUnitPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
            {
                e.Handled = true;
            }
        }

        // 计量单位改变时，改变相应的单价
        private void cbbUnitName_SelectedIndexChanged(object sender, EventArgs e)
        {
            decimal Quantity = 0;
            decimal AssQuantity = 0;

            if (gInventory != null)
            {
                //if (cbbUnitName.SelectedIndex == 0)
                //{
                //    Quantity = Convert.ToDecimal(txtQuantity.Text.Trim());
                //    if (gInventory.AssitUnitRate > 0)
                //        AssQuantity = Quantity / gInventory.AssitUnitRate;
                //    txtAssitQuantity.Text = string.Format("{0:#0.##}", AssQuantity);
                //    txtUnitPrice.Text = ConvertUtil.RoundToStr(gInventory.RectInPrice, 3);
                //    txtPrice.Text = ConvertUtil.RoundToStr(gInventory.RectInPrice * Quantity, 2);
                //    lblAssitQuantity.Text = "件数：";
                //}
                //else if (cbbUnitName.SelectedIndex == 1)
                //{
                //    AssQuantity = Convert.ToDecimal(txtQuantity.Text.Trim());
                //    if (gInventory.AssitUnitRate > 0)
                //        Quantity = AssQuantity * gInventory.AssitUnitRate;
                //    txtAssitQuantity.Text = ConvertUtil.RoundToStr(Quantity, 2); ;
                //    txtUnitPrice.Text = ConvertUtil.RoundToStr(gInventory.AssitInPrice, 3);
                //    txtPrice.Text = ConvertUtil.RoundToStr(gInventory.AssitInPrice * AssQuantity, 2);
                //    lblAssitQuantity.Text = gInventory.UnitName;
                //}
            }
        }

        private ListViewItem GetInventoryItem(string BarCode)
        {
            ListViewItem lvItem = null;
            foreach (ListViewItem lvi in lstData.Items)
            {
                if (lvi.Text == BarCode.Trim())
                {
                    lvItem = lvi;
                    break;
                }
            }
            return lvItem;
        }

        private void ClearEditValue()
        {
            txtBarCode.Text = "";
            txtInvName.Text = "";
            txtInvStd.Text = "";
            txtInvType.Text = "";
            txtAssitUnitRate.Text = "";
            txtQuantity.Text = "";
            txtAssitQuantity.Text = "";
            cbbUnitName.Items.Clear();
            txtUnitPrice.Text = "";
            txtPrice.Text = "";
        }

        private void AddInventoryList()
        {
            Boolean IsExist = false;

            if (gInventory != null)
            {
                // 若该条码已经存在，则更改数量与金额即可。
                foreach (ListViewItem lvItem in lstData.Items)
                {
                    if (lvItem.Text == txtBarCode.Text)
                    {
                        decimal Value = 0;
                        Value = Convert.ToDecimal(lvItem.SubItems[5].Text) + Convert.ToDecimal(txtQuantity.Text);
                        lvItem.SubItems[5].Text = ConvertUtil.RoundToStr(Value, 2);

                        Value = Convert.ToDecimal(lvItem.SubItems[7].Text) + Convert.ToDecimal(txtPrice.Text);
                        lvItem.SubItems[7].Text = ConvertUtil.RoundToStr(Value, 2);
                        IsExist = true;
                        break;
                    }
                }

                // 该条码不存在，则添加记录
                if (!IsExist)
                {
                    ListViewItem item = new ListViewItem(new string[] { txtBarCode.Text, txtInvName.Text, txtInvStd.Text, txtInvType.Text, 
                            cbbUnitName.Text, txtQuantity.Text, txtUnitPrice.Text, txtPrice.Text });
                    lstData.Items.Add(item);
                }
            }
        }

        private void btnScanOK_Click(object sender, EventArgs e)
        {
            AddInventoryList();
            ClearEditValue();
        }

        private void btnGetVouchCode_Click(object sender, EventArgs e)
        {
            txtVouchCode.Text = VouchEntity.GetVouchCode(VouchType.StockInPut);
        }

        private void txtVouchCode_GotFocus(object sender, EventArgs e)
        {
            if (txtVouchCode.Text == string.Empty)
                btnGetVouchCode.PerformClick();
        }


    }
}
